<?php

	class Controller
	{
		private static $sView;
		
		private function __construct() {}
		private function __destruct() {}
		private function __clone() {}
		
		public static function process()
		{
			self::$sView = self::view();
			
			self::$sView->addCss("style"); 
			//$view->html();
		}
		
		public static function generatePage()
		{
			self::$sView->html();
		}
		
		private static function view()
		{
			$map = array
			(
				"main" => array
					(
						"auth" => false,
						"view" => "MainView",
						"model" => array("register" => "RegisterModel", "login" => "LoginModel"),
					),
				"register" => array
					(
						"auth" => false,
						"view" => "RegisterView",
						"model" => array("register" => "RegisterModel"),
					),
				"login" => array
					(
						"auth" => false,
						"view" => "LoginView",
						"model" => array("login" => "LoginModel"),
					),
				"home" => array
					(
						"auth" => true,
						"ac_level" => Access::PLAYER,
						"auth_redirect" => "login",
						"view" => "HomeView",
						"model" => "HomeModel",
					),
				"city" => array
					(
						"auth" => true,
						"ac_level" => Access::PLAYER,
						"auth_redirect" => "login",
						"view" => "CityView",
						"model" => "CityModel",
					),
				"adminlogin" => array
					(
						"auth" => false,
						"view" => "AdminLoginView",
						"model" => "AdminLoginModel",
					),
				"admin" => array
					(
						"auth" => true,
						"ac_level" => Access::ADMIN,
						"auth_redirect" => "adminlogin",
						"view" => "AdminView",
						"model" => "AdminModel",
					),
				"items_edit" => array
					(
						"auth" => true,
						"ac_level" => Access::ADMIN,
						"auth_redirect" => "adminlogin",
						"view" => "ItemsEditView",
						"model" => "ItemsEditModel",
					),
			);
			$view = null;
			if (Input::hasRequest("view"))
				$view = Input::request("view");
			if (empty($view))
				$view = "main";
			$view = &$map[$view];
			
			// Если требуется авторизация, но её нет
			if ($view["auth"])
			{
				$auth = null;
				if (Session::has("auth"))
					$auth = Session::get("auth");
				if (is_null($auth) || $auth["ac_level"] < $view["ac_level"])
				{
					header(sprintf("Location: /?view=%s&pattern=need&redirect=%s", $view["auth_redirect"], 
						Input::request("view")));
					die();
				}
			}
			
			$model = null;
			
			if (!is_array($view["model"]))
				$model = $view["model"];
			else if (Input::hasRequest("action") && isset($view["model"][Input::request("action")]))
				$model = $view["model"][Input::request("action")];

			if (is_null($model) && isset($view["model"]["#def"]))
				$model = &$view["model"]["#def"];
	
			if (is_null($model)) // Если для представления нет даже дефолтного экшена то создать только его
				return new $view["view"]();
			return new $view["view"](new $model()); // Если есть, создать экшен и передать его представлению
		}
	}

?>